package com.service.db.impl;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.text.MessageFormat;
import java.util.List;

import com.service.bean.alarm.AlarmInfo;
import com.service.bean.alarm.DutyAlarm;
import com.service.bean.alarm.HookArmAlarm;
import com.service.bean.alarm.WorkTimes;
import com.service.bean.company.Company;
import com.service.cache.CacheManager;
import com.service.db.DbConnectionManager;
import com.service.db.PooledConnection;
import com.service.other.Logger;

public class DbAlarm {
	
	/**
	 * 批量保存报警对象
	 */
	public void batchSaveAlarmInfo(Company company) {
		if (CacheManager.instance().getAlarmSaveQueue(company).size()==0) {
			return;
		}
		PooledConnection con = null;
		Statement pstmt = null;
		try {
			int x = 0;
			boolean isEnd = false;
			con = DbConnectionManager.getConnection(company);
			con.setAutoCommit(false); 
			pstmt = con.prepareStatement();
			String sql = "INSERT INTO crc_GPSAlarm (SignalId,GPSNO,EquipmentId,AlarmTime,AlarmLocation,"
					+ "AlarmType,AlarmSubType,CompanyCode,AlarmLevel,AlarmInfo,AreaId,RefId,KeepTime,AddTime) "
					+ "VALUES ";
			do{
				AlarmInfo ai = CacheManager.instance().getAlarmSaveQueue(company).poll();
				pstmt.addBatch(sql+MessageFormat.format("({0},{1},{2},{3},{4},{5},{6},{7},{8},{9},{10},{11},{12},{13})",ai.getAll()));
				isEnd = CacheManager.instance().getAlarmSaveQueue(company).isEmpty();
				x++;
				if(x==1000||isEnd){
					long y = System.currentTimeMillis();
					pstmt.executeBatch();
					con.commit();
					pstmt.clearBatch();
					long z = System.currentTimeMillis();
					Logger.println("[" + company.companyName + "]成功存储"+x+"条alarm数据,用时"+(z-y)+"毫秒.");
					System.out.println("["+company.companyName+"]管道中剩余"+CacheManager.instance().getAlarmSaveQueue(company).size()+"条数据");
					x=0;
				}
			} while (!isEnd);
			con.setAutoCommit(true);
		} catch (SQLException e) {
			Logger.err(e);
		} finally {
			try {
				if (pstmt != null) {
					pstmt.close();
				}
				if (con != null) {
					con.close();
				}
			} catch (SQLException e) {
				Logger.err(e);
			}
		}
	}
	
	/**
	 * 批量保存勾臂车箱体满溢报警数据
	 */
	public void batchSaveHookArmAlarmInfo(Company company) {
		if (CacheManager.instance().getHookArmAlarmSaveQueue(company).isEmpty())
			return;
		PooledConnection con = null;
		Statement pstmt = null;
		try {
			int x = 0;
			boolean isEnd = false;
			con = DbConnectionManager.getConnection(company);
			con.setAutoCommit(false); 
			pstmt = con.prepareStatement();
			String sql = "INSERT INTO crc_GPSHookArmCaseAlarm (SignalId,GPSNO,CaseId,AlarmTime,"
					+ "AlarmInfo,CompanyCode,BaiduLng,BaiduLat,Status,KeepTime,CaseName) VALUES ";
			do {
				HookArmAlarm alarm = CacheManager.instance().getHookArmAlarmSaveQueue(company).poll();
				pstmt.addBatch(sql+MessageFormat.format("({0},{1},{2},{3},{4},{5},{6},{7},{8},{9},{10})", alarm.getAll()));
				isEnd = CacheManager.instance().getHookArmAlarmSaveQueue(company).isEmpty();
				x++;
				if (x == 1000 || isEnd) {
					long y = System.currentTimeMillis();
					pstmt.executeBatch();
					con.commit();
					pstmt.clearBatch();
					long z = System.currentTimeMillis();
					Logger.println("[" + company.companyName + "]成功存储" + x + "条hook arm alarm数据,用时"+(z-y)+"毫秒.");
					System.out.println("["+company.companyName+"]管道中剩余"+CacheManager.instance().getHookArmAlarmSaveQueue(company).size()+"条数据");
					x = 0;
				}
			} while (!isEnd);
		} catch (Exception e) {
			Logger.err(e);
		} finally {
			try {
				if (null != pstmt)
					pstmt.close();
				if (null != con)
					con.close();
			} catch (Exception ex) {
				Logger.err(ex);
			}
		}
	}
	
	public void batchSaveWorkTimes(List<WorkTimes> list, Company company) {
		if (null == list || 0 == list.size())
			return;
		PooledConnection con = null;
		Statement pstmt = null;
		try {
			con = DbConnectionManager.getConnection(company);
			con.setAutoCommit(false); 

			pstmt = con.prepareStatement();
			String sql = "INSERT INTO crc_EquipmentJobTime (EquipmentId,AreaId,RoadId,ReportDate,PlanJobTime,ActualJobTime) VALUES ";
			for (WorkTimes wt : list) {
				if (null == wt)
					continue;
				pstmt.addBatch(sql+MessageFormat.format("({0},{1},{2},{3},{4},{5})",wt.getAll()));
			}
			pstmt.executeBatch();
			con.commit();
			pstmt.clearBatch();
			Logger.println("成功存储" + list.size() + "作业遍数数据");
		} catch (Exception e) {
			Logger.err(e);
		} finally {
			try {
				if (null != pstmt)
					pstmt.close();
				if (null != con)
					con.close();
			} catch (Exception ex) {
				Logger.err(ex);
			}
		}
	}
	
	/**
	 * 保存出勤报警信息
	 * @param dutyAlarm
	 */
	public void saveDutyAlarm(DutyAlarm dutyAlarm, Company company) {
		PooledConnection con = null;
		PreparedStatement pstmt = null;
		String sql = "INSERT INTO crc_EquipmentAttendance "
				+ "(EquipmentId,SchedId,AlarmTime,AttendanceStatus) VALUES (?,?,?,?)";
		try {
			con = DbConnectionManager.getConnection(company);

			pstmt = con.prepareStatement(sql);
			pstmt.setString(1, dutyAlarm.equipmentId);
			pstmt.setInt(2, dutyAlarm.schedId);
			pstmt.setTimestamp(3, new Timestamp(dutyAlarm.alarmDate.getTime()));
			pstmt.setInt(4, dutyAlarm.attendanceStatus);
			pstmt.execute();
		} catch (Exception e) {
			Logger.err(e);
		} finally {
			try {
				if (null != pstmt)
					pstmt.close();
				if (null != con)
					con.close();
			} catch (Exception ex) {
				Logger.err(ex);
			}
		}
	}
	
}
